package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.Product;
import com.spzx.product.domain.ProductDetails;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.domain.SkuStock;
import com.spzx.product.mapper.*;
import com.spzx.product.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author whxfc
 * 商品Service业务层处理
 */
@Service
@Slf4j
public class ProductServiceImpl
        extends ServiceImpl<ProductMapper, Product>
        implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;
    @Autowired
    private SkuStockMapper skuStockMapper;
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Product> selectProductList(Product product) {
        List<Product> list = productMapper.selectProductList(product);
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertProduct(Product product) {
        //product表
        productMapper.insert(product);

        //product_sku表
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (int i = 0; i < productSkuList.size(); i++) {
            ProductSku productSku = productSkuList.get(i);
            productSku.setId(null);
            productSku.setSkuCode(product.getId() + "_" + i);
            productSku.setProductId(product.getId());
            productSku.setSkuName(product.getName() + " " + productSku.getSkuSpec());
            productSku.setStatus(0);
            productSkuMapper.insert(productSku);

            //sku_stock表
            SkuStock skuStock = new SkuStock();
            skuStock.setSkuId(productSku.getId());
            skuStock.setTotalNum(productSku.getStockNum());
            skuStock.setLockNum(0);
            skuStock.setSaleNum(0);
            skuStock.setAvailableNum(productSku.getStockNum());
            skuStock.setStatus(0);
            skuStockMapper.insert(skuStock);
        }
        //product_details
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        //将数组转化为字符串用","拼接
        productDetails.setImageUrls(String.join(",", product.getDetailsImageUrlList()));
        productDetailsMapper.insert(productDetails);

        return product.getId().intValue();
    }

    /**
     * 根据id查询商品详细信息
     *
     * @param id 商品主键
     * @return
     */
    @Override
    public Product selectProductDetailsById(Long id) {
        Product product = productMapper.selectProductDetailsById(id);

        ProductDetails productDetails = productDetailsMapper.selectOne(new LambdaQueryWrapper<ProductDetails>().eq(ProductDetails::getProductId, id));
        if (productDetails == null) {
            throw new ServiceException("该商品不存在");
        }
        product.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        return product;
    }

    /**
     * 修改商品信息
     *
     * @param product 商品
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateProductDetails(Product product) {
        //删除product
        productMapper.deleteById(product);
        //删除product_details
        productDetailsMapper.delete(new LambdaQueryWrapper<ProductDetails>().eq(ProductDetails::getProductId, product.getId()));
        //删除stu_stock
        List<ProductSku> productSkus = productSkuMapper.selectList(new LambdaQueryWrapper<ProductSku>().eq(ProductSku::getProductId, product.getId()));
        List list = new ArrayList();
        for (ProductSku skus : productSkus) {
            list.add(skus.getId());
        }
        System.out.println("list = " + list);
        skuStockMapper.delete(new LambdaQueryWrapper<SkuStock>().in(SkuStock::getSkuId, list));

        //删除product_sku
        productSkuMapper.delete(new LambdaQueryWrapper<ProductSku>().eq(ProductSku::getProductId, product.getId()));

        //重新添加
        product.setId(null);
        this.insertProduct(product);
        return 1;
    }

    /**
     * 批量删除商品
     *
     * @param ids 需要删除的商品主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteProductByIds(Long[] ids) {
        //删除product
        productMapper.deleteBatchIds(Arrays.asList(ids));
        //删除product_details
        productDetailsMapper.delete(new LambdaQueryWrapper<ProductDetails>().in(ProductDetails::getProductId, ids));

        //删除sku_stock
        //获取sku列表
        List<ProductSku> productSkuList = productSkuMapper.selectList(new LambdaQueryWrapper<ProductSku>().in(ProductSku::getProductId, ids));
        List<Long> productSkuIdList = productSkuList.stream().map(ProductSku::getId).collect(Collectors.toList());
        skuStockMapper.delete(new LambdaQueryWrapper<SkuStock>().in(SkuStock::getId, productSkuIdList));
        //删除product_sku
        productSkuMapper.delete(new LambdaQueryWrapper<ProductSku>().in(ProductSku::getId, productSkuIdList));
        return 1;
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        if (auditStatus == 1) {
            product.setAuditStatus(1);
            product.setAuditMessage("审核通过");
        } else if (auditStatus == -1) {
            product.setAuditStatus(-1);
            product.setAuditMessage("审核不通过");
        } else {
            product.setAuditStatus(0);
            product.setAuditMessage("初始状态");
        }
        productMapper.updateById(product);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        ProductSku productSku = new ProductSku();
        SkuStock skuStock = new SkuStock();
        product.setId(id);
        if (status == 1) {
            productSku.setStatus(1);
            product.setStatus(1);
            skuStock.setStatus(1);
        } else if (status == -1) {
            product.setStatus(-1);
            productSku.setStatus(-1);
            skuStock.setStatus(-1);
        } else {
            product.setStatus(0);
            productSku.setStatus(0);
            skuStock.setStatus(0);
        }
        productMapper.updateById(product);
        //获取sku列表
        List<ProductSku> productSkuList = productSkuMapper.selectList(new LambdaQueryWrapper<ProductSku>().eq(ProductSku::getProductId, id));
        List<Long> productSkuIdList = productSkuList.stream().map(ProductSku::getId).collect(Collectors.toList());
        for (Long aLong : productSkuIdList) {
            productSku.setId(aLong);
            skuStock.setId(aLong);
            productSkuMapper.updateById(productSku);
            skuStockMapper.updateById(skuStock);
        }
    }
}
